package com.siao.component
{
	import flash.events.Event;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	/**
	 * 缩略文本，超出显示范围的文本将用省略符代替
	 * @author SiaoLeon
	 */
	public class ThumbTextField extends TextField
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		private var _text:String;
		
		override public function get text():String
		{
			return this._text;
		}
		
		override public function set text(value:String):void
		{
			this._text = value;
			
			this.formatText();
		}
		
		override public function set width(value:Number):void
		{
			super.width = value;
			
			this.formatText();
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			
			this.formatText();
		}
		
		override public function set defaultTextFormat(format:TextFormat):void
		{
			super.defaultTextFormat = format;
			
			this.formatText();
		}
		
		override public function set multiline(value:Boolean):void
		{
			super.multiline = value;
			
			this.formatText();
		}
		
		override public function set wordWrap(value:Boolean):void
		{
			super.wordWrap = value;
			
			this.formatText();
		}
		
		override public function set styleSheet(value:StyleSheet):void
		{
			super.styleSheet = value;
			
			this.formatText();
		}
		
		override public function set maxChars(value:int):void
		{
			super.maxChars = value;
			
			this.formatText();
		}
		
		override public function set restrict(value:String):void
		{
			super.restrict = value;
			
			this.formatText();
		}
		
		override public function set antiAliasType(antiAliasType:String):void
		{
			super.antiAliasType = antiAliasType;
			
			this.formatText();
		}
		
		override public function set embedFonts(value:Boolean):void
		{
			super.embedFonts = value;
			
			this.formatText();
		}
		
		override public function set filters(value:Array):void
		{
			super.filters = value;
			
			this.formatText();
		}
		
		override public function set gridFitType(gridFitType:String):void
		{
			super.gridFitType = gridFitType;
			
			this.formatText();
		}
		
		override public function set sharpness(value:Number):void
		{
			super.sharpness = value;
			
			this.formatText();
		}
		
		override public function set thickness(value:Number):void
		{
			super.thickness = value;
			
			this.formatText();
		}
		
		override public function set type(value:String):void
		{
			super.type = value;
			
			this.formatText();
		}
		
		private var _isThumb:Boolean;
		
		public function get isThumb():Boolean
		{
			return this._isThumb;
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var ellipsis:String;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 * @param ellipsis 省略符文本
		 */
		public function ThumbTextField(ellipsis:String="...")
		{
			super();
			
			this.ellipsis = ellipsis;
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true);
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		/**
		 * 添加到舞台时
		 * @param event
		 */
		private function onAddedToStage(event:Event):void
		{
			this.formatText();
		}
		
		/**
		 * 格式化文本
		 */
		private function formatText():void
		{
			this._isThumb = false;
			
			//以下情况是不缩略多余文本
			//	当文本框为输入文本框时
			//	当文本框已密码形式显示时
			if (this.type == TextFieldType.INPUT || this.displayAsPassword)
			{
				super.text = (this.text == null) ? "" : this.text;
				return;
			}
			
			//获取显示最大高度
			var maxHeight:Number = (this.multiline || this.wordWrap) ? this.height : this.getLineMetrics(0).height;
			
			//创建测试用多行文本框
			var txtMultiLine:TextField = new TextField();
			txtMultiLine.defaultTextFormat = this.defaultTextFormat;
			txtMultiLine.styleSheet = this.styleSheet;
			txtMultiLine.antiAliasType = this.antiAliasType;
			txtMultiLine.embedFonts = this.embedFonts;
			txtMultiLine.filters = this.filters;
			txtMultiLine.gridFitType = this.gridFitType;
			txtMultiLine.sharpness = this.sharpness;
			txtMultiLine.thickness = this.thickness;
			txtMultiLine.maxChars = this.maxChars;
			txtMultiLine.restrict = this.restrict;
			txtMultiLine.multiline = txtMultiLine.wordWrap = true;
			txtMultiLine.autoSize = TextFieldAutoSize.LEFT;
			txtMultiLine.width = this.width;
			txtMultiLine.height = this.height;
			txtMultiLine.text = (this.text == null) ? "" : this.text;
			
			//不超出显示范围
			var isOver:Boolean = (this.multiline || this.wordWrap) ? txtMultiLine.height > this.height : txtMultiLine.numLines > 1;
			
			if (!isOver)
			{
				super.text = (this.text == null) ? "" : this.text;
				return;
			}
			
			//超出显示范围的整行移除
			for (var i:int = txtMultiLine.numLines; i > 2; i--)
			{
				txtMultiLine.text = txtMultiLine.text.slice(0, txtMultiLine.getLineOffset(i - 1));
				
				if (txtMultiLine.height <= maxHeight)
				{
					break;
				}
			}
			
			//添加省略符号后依次减少最后文本字符
			var thumbText:String  = txtMultiLine.text;
			txtMultiLine.text = thumbText + this.ellipsis;
			
			while (txtMultiLine.height > maxHeight && txtMultiLine.numLines > 1)
			{
				thumbText = thumbText.substring(0, thumbText.length - 1);
				txtMultiLine.text = thumbText + this.ellipsis;
			}
			
			super.text = txtMultiLine.text;
			
			this._isThumb = true;
		}
	
	}
}
